package models.serializer;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;

import models.Comment;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class CommentJsonSerializer implements JsonSerializer<Comment> {

	public static CommentJsonSerializer instance;
	
	private CommentJsonSerializer() {
		
	}
	
	public static CommentJsonSerializer get() {
        if (instance == null) {
            instance = new CommentJsonSerializer();
        }
        return instance;
    }
	
	@Override
	public JsonElement serialize(Comment comment, Type type, JsonSerializationContext jsonSerializationContext) {
		DateFormat formatter = new SimpleDateFormat("dd.MM.yy - HH:mm");
		
		JsonObject obj = new JsonObject();
        obj.add("postedBy", jsonSerializationContext.serialize(comment.postedBy));
        obj.addProperty("postedAt", formatter.format(comment.postedAt));
        obj.addProperty("id", comment.id);
        obj.addProperty("content", comment.content);
        //obj.add("lecture", jsonSerializationContext.serialize(comment.lecture));
        //obj.add("question", jsonSerializationContext.serialize(comment.question));
        return obj;
	}
    
}
